package com.fast.configs.properties;

import cn.hutool.core.util.StrUtil;
import com.fast.constants.CommonConstants;
import lombok.Data;
import org.apache.commons.io.FileUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;

/**
 * @author ruan cai yuan
 * @version 1.0
 * @fileName com.fast.configs.file.properties.UploadProperties
 * @description: TODO
 * @since 2024/6/22 1:43
 */
@ConfigurationProperties(prefix = "upload")
@Data
public class UploadProperties {
    /**
     * 存储文件的绝对路径
     */
    private String staticDir;

    @PostConstruct
    public void init() {
        if (StrUtil.isBlank(staticDir)) {
            throw new RuntimeException("UploadProperties.staticDir 未配置");
        }
        if (staticDir.endsWith(CommonConstants.SLASH)) {
            throw new RuntimeException("UploadProperties.staticDir 没有以/结束");
        }
        File temp = new File(staticDir);
        if (!temp.isAbsolute()) {
            throw new RuntimeException("UploadProperties.staticDir 不是绝对路径");
        }

        if (!temp.exists()) {
            try {
                FileUtils.forceMkdir(temp);
            } catch (IOException e) {
                throw new RuntimeException("创建上传目录出错");
            }
        }
    }
}
